package com.lin.missyou.dto.validators;

import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义效验注解步骤
 *      1、创建注解类
 *      2、创建验证器并实现 ConstraintValidator 接口
 *          参数一：注解类
 *          参数二：效验的数据类型
 *      3、重写 ConstraintValidator 两个方法
 *          initialize 注解参数验证
 *          isValid 是否通过
 * @author gcq
 * @Create 2021-10-29
 */
public class TokenPasswordValidator implements ConstraintValidator<TokenPassword, String>
{

    private Integer min;
    private Integer max;

    @Override
    public void initialize(TokenPassword constraintAnnotation)
    {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext context)
    {
        if(StringUtils.isEmpty(s))
        {
            return true;
        }
        return s.length() >= this.min && s.length() <= this.max;
    }
}